﻿using UnityEngine;
using System.Collections;

public class ChangeGauge : MonoBehaviour {
	
	float lowest = 0;//0.76f;
	UISprite sprite;
	// Use this for initialization
	void Start () {
		sprite = GetComponent<UISprite>();
	}
	
	public void SetGauge(float va, float max_va, float delay)
	{
		if(sprite==null)
			sprite = GetComponent<UISprite>();
		float fill = va/max_va;
		fill = lowest + (1-lowest)*fill;
		Hashtable ht = iTween.Hash("from",sprite.fillAmount, "to", fill ,"time",0.7f, "delay", delay,  "onupdate","ChangeUpdate");
		iTween.ValueTo(gameObject,ht);
	}
	
	public void SetGaugeFix(int mp, bool instantChange)
	{
		if(sprite==null)
			sprite = GetComponent<UISprite>();
        float[] ratio = {0, 0.168f, 0.336f, 0.5f, 0.668f, 0.836f, 1};
		if(mp>6)
			mp=6;
		if(instantChange)
			sprite.fillAmount = ratio[mp];
		else {
			Hashtable ht = iTween.Hash("from",sprite.fillAmount, "to", ratio[mp] ,"time",0.5f,  "onupdate","ChangeUpdate");
			iTween.ValueTo(gameObject,ht);		
		}
	}
	
	void ChangeUpdate(float newValue)
	{
		//glog.debug(gameObject.name+ "  newBalue="+newValue);
		sprite.fillAmount = newValue;
	}
	
}
